//
//  FeatureViewController.m
//  Korea
//
//  Created by 崔剑 on 15/8/24.
//  Copyright (c) 2015年 KoreaGo. All rights reserved.
//

#import "FeatureViewController.h"
#import "FeatureInfoViewController.h"
#import "FeatureNewViewController.h"
#import "Header.h"
#import "FeatureCell.h"
#import "FeatureNew.h"

@interface FeatureViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *featureTableView;
@property (nonatomic, strong) NSMutableArray *sourceArr;
@property (nonatomic, strong) MBProgressHUD *HUD;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, copy) NSString *str;

@end

@implementation FeatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"每日专辑";
    
    self.featureTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 113) style:UITableViewStylePlain];
    self.featureTableView.backgroundColor = [UIColor colorWithRGB:0xf3f4f8 alpha:1];
    
    // 隐藏滑动条
    self.featureTableView.showsVerticalScrollIndicator = NO;
    self.featureTableView.delegate = self;
    self.featureTableView.dataSource = self;
    [self.view addSubview:self.featureTableView];
    // 自适应屏幕
    self.edgesForExtendedLayout = UIRectEdgeNone;
    // 注册重用池
    [self.featureTableView registerClass:[FeatureCell class] forCellReuseIdentifier:@"featureReuse"];
    self.sourceArr = [NSMutableArray array];
    self.str = [[NSString alloc] init];
    
    [self handleData];
    
}

// 网络请求
- (void)handleData
{
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //常用的设置
    //小矩形的背景色
    self.HUD.color = [UIColor blackColor];//这儿表示无背景
    //显示的文字
    self.HUD.labelText = @"加载中";
    self.HUD.dimBackground = YES;
    
    
    NSString *str = @"http://192.168.0.3:8081/rest/album/?days=5&size=10";
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [AFNTool get:str Body:nil HttpResult:JSON success:^(id result) {
        
        self.dataArr = result;
//        NSMutableArray *arr = [NSMutableArray array];
//        FeatureNew *featureNew = [[FeatureNew alloc] init];
//        FeatureNew *featureHot = [[FeatureNew alloc] init];
//
//        for (NSDictionary *dic in result) {
//            NSMutableDictionary *new = dic[@"item"];
//            NSMutableDictionary *hot = dic[@"hotItem"];
//          featureNew = [FeatureNew modelArrWithDic:new];
//            featureHot = [FeatureNew modelArrWithDic:hot];
//            [arr addObject:featureNew];
//    
//        }
//        NSLog(@"%@", arr);
        
        [self.featureTableView reloadData];
        [self.HUD hide:YES];
        
    } failure:^(NSError *error) {
        [self.HUD hide:YES];
        
        
    }];
    
}


// 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"点击cell");
    if (indexPath.row == 0) {
        
        FeatureNewViewController *featureNewVC = [[FeatureNewViewController alloc] init];
        featureNewVC.page = indexPath.section;
        [self.navigationController pushViewController:featureNewVC animated:YES];
        
    } else {
    
    FeatureInfoViewController *featureHotVC = [[FeatureInfoViewController alloc] init];
    featureHotVC.page = indexPath.section;
    [self.navigationController pushViewController:featureHotVC animated:YES];
    }
    
    
}

// 分区的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count;
}

// 每个分区的header高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    headerView.backgroundColor = [UIColor whiteColor];
    
    self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, self.view.frame.size.width / 3, 20)];
    self.timeLabel.textColor = [UIColor blackColor];
    self.timeLabel.font = [UIFont boldSystemFontOfSize:13];
    self.timeLabel.text = self.str;
    [headerView addSubview:self.timeLabel];
    
    
    
    if (section == 0) {
        
        UILabel *nextTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2, 0, self.view.frame.size.width / 2 - 15, 40)];
        nextTimeLabel.textColor = [UIColor colorWithRGB:0xfe868f alpha:1];
        nextTimeLabel.textAlignment = NSTextAlignmentRight;
        nextTimeLabel.font = [UIFont boldSystemFontOfSize:13];
        nextTimeLabel.text = @"下次更新8:00";
        [headerView addSubview:nextTimeLabel];
    }
    
    return headerView;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return 10;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 10)];
    footView.backgroundColor = [UIColor colorWithRGB:0xf3f4f8 alpha:1];
    return footView;

    
    
}
//去掉UItableview headerview黏性
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.featureTableView)
    {
        CGFloat sectionHeaderHeight = 40;
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // cell高度
    return self.view.height / 3;
    
}

#pragma mark UITabelViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *dic = self.dataArr[section];
    if ([dic objectForKey:@"hotItem"] == [NSNull null]) {
        
        return 1;
        
    }
    
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FeatureCell *cell = [self.featureTableView dequeueReusableCellWithIdentifier:@"featureReuse"];
    // 点击cell不变色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSMutableDictionary *newDic = [self.dataArr[indexPath.section] objectForKey:@"item"];

    if ([self.dataArr[indexPath.section] objectForKey:@"hotItem"] == [NSNull null]) {
        cell.titleLabel.text = [newDic objectForKey:@"title"];
        [cell.featureImage sd_setImageWithURL:[NSURL URLWithString:[newDic objectForKey:@"image"]]];
        self.str = [[newDic objectForKey:@"updated"] substringToIndex:11];
       

    } else {
        
        if (indexPath.row == 0) {
            
            cell.titleLabel.text = [newDic objectForKey:@"title"];
            [cell.featureImage sd_setImageWithURL:[NSURL URLWithString:[newDic objectForKey:@"image"]]];
            NSString *timeStr = [[newDic objectForKey:@"updated"] substringToIndex:11];
            self.timeLabel.text = timeStr;
            
        } if (indexPath.row == 1) {
           
            cell.titleLabel.text = [[self.dataArr[indexPath.section] objectForKey:@"hotItem"] objectForKey:@"title"];
            [cell.featureImage sd_setImageWithURL:[NSURL URLWithString:[[self.dataArr[indexPath.section] objectForKey:@"hotItem"] objectForKey:@"image"]]];
            NSString *timeStr = [[[self.dataArr[indexPath.section] objectForKey:@"hotItem"] objectForKey:@"updated"] substringToIndex:11];
            self.timeLabel.text = timeStr;
        }
        
    }
    

    
    return cell;
}




- (void)viewWillAppear:(BOOL)animated
{
    
    self.tabBarItem.title = @"专辑";
    
}


@end
